package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class MenusSlice extends AbilitySlice {

    private Image back_button, fender_checkbox, schecter_checkbox, gibson_checkbox,
            hagstrom_checkbox, lbanez_checkbox, image_title_one, image_title_two, image_title_three,
            menu_back_button, news_image, mail_image, profile_image;
    private Text text_title;
    private DirectionalLayout fender_lin, schecter_lin, gibson_lin,
            hagstrom_lin, lbanez_lin, dialog_menus, menus_layout;
    private Button show_menu;
    private RadioButton radioButton_one, radioButton_three, radioButton_two;
    private RadioContainer radioContainer;
    private Text schecter_text, hagstrom_text;
    private boolean flag_layout = false;
    private boolean showMenu = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_menus);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0){
            if (MainAbilitySlice.all.containsKey("Menus")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Menus")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Menus", integer);
                        pf.flushSync();
                    }
                }
            }else {
                pf.putInt("Menus", 1);
                pf.flushSync();
            }
        }else {
            pf.putInt("Menus", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        show_menu = (Button) findComponentById(ResourceTable.Id_menu_btn);
        menu_back_button = (Image) findComponentById(ResourceTable.Id_menu_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        image_title_one = (Image) findComponentById(ResourceTable.Id_menu_image1);
        image_title_two = (Image) findComponentById(ResourceTable.Id_menu_image2);
        image_title_three = (Image) findComponentById(ResourceTable.Id_menu_image3);
        schecter_text = (Text) findComponentById(ResourceTable.Id_schecter_text);
        hagstrom_text = (Text) findComponentById(ResourceTable.Id_hagstrom_text);
        news_image = (Image) findComponentById(ResourceTable.Id_news_image_dialog);
        mail_image = (Image) findComponentById(ResourceTable.Id_mail_image_dialog);
        profile_image = (Image) findComponentById(ResourceTable.Id_profile_image_dialog);
        menus_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_menus_layout);
        dialog_menus = (DirectionalLayout) findComponentById(ResourceTable.Id_dialog_menus);
        news_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_new_releases_black_24dp));
        mail_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_email));
        profile_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_person_black_24dp));
        VectorElement element = new VectorElement(getContext(), ResourceTable.Graphic_ic_new_releases_black_24dp);
        VectorElement element_email = new VectorElement(getContext(), ResourceTable.Graphic_ic_email_black_24dp);
        VectorElement element_person = new VectorElement(getContext(), ResourceTable.Graphic_ic_person_black_24dp);
        image_title_three.setVisibility(Component.VISIBLE);
        image_title_one.setImageElement(element_email);
        image_title_two.setImageElement(element_person);
        image_title_three.setImageElement(element);

        initCheckbox();
        initRadiobutton();

        text_title.setText("Menus");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        menu_back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        menu_back_button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
//        show_menu.setClickedListener(component -> dialog_menus.setVisibility(Component.VISIBLE));
        show_menu.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!showMenu){
                    dialog_menus.setVisibility(Component.VISIBLE);
                    showMenu = true;
                }else {
                    dialog_menus.setVisibility(Component.HIDE);
                    showMenu = false;
                }
            }
        });
        menus_layout.setClickedListener(component -> dialog_menus.setVisibility(Component.HIDE));
        back_button.setClickedListener(component -> onBackPressed());
    }

    private void initCheckbox() {
        fender_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_fender_lin);
        schecter_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_schecter_lin);
        gibson_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_gibson_lin);
        hagstrom_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_hagstom_lin);
        lbanez_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_lbanez_lin);
        fender_checkbox = (Image) findComponentById(ResourceTable.Id_fender_checkbox);
        schecter_checkbox = (Image) findComponentById(ResourceTable.Id_schecter_checkbox);
        gibson_checkbox = (Image) findComponentById(ResourceTable.Id_gibson_checkbox);
        hagstrom_checkbox = (Image) findComponentById(ResourceTable.Id_hagstom_checkbox);
        lbanez_checkbox = (Image) findComponentById(ResourceTable.Id_lbanez_checkbox);
        fender_lin.setClickedListener(component -> {
            if (fender_checkbox != null && flag_layout == false) {
                fender_checkbox.setPixelMap(ResourceTable.Media_checkbox_true);
                flag_layout = true;
            } else {
                fender_checkbox.setPixelMap(ResourceTable.Media_checkbox_false);
                flag_layout = false;
            }
        });
        schecter_lin.setClickedListener(component -> {
            if (schecter_checkbox != null && flag_layout == false && schecter_text != null) {
                schecter_checkbox.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                schecter_text.setTextColor(new Color(0xffffb90f));
                flag_layout = true;

            } else {
                schecter_checkbox.setPixelMap(ResourceTable.Media_checkbox_false);
                schecter_text.setTextColor(new Color(0x99999999));
                flag_layout = false;
            }
        });
        gibson_lin.setClickedListener(component -> {
            if (gibson_checkbox != null && flag_layout == false) {
                gibson_checkbox.setPixelMap(ResourceTable.Media_checkbox_true);
                flag_layout = true;
            } else {
                gibson_checkbox.setPixelMap(ResourceTable.Media_checkbox_false);
                flag_layout = false;
            }
        });
        hagstrom_lin.setClickedListener(component -> {
            if (hagstrom_checkbox != null && flag_layout == false && hagstrom_text != null) {
                hagstrom_checkbox.setImageAndDecodeBounds(ResourceTable.Media_checkbox_yellow_true);
                hagstrom_text.setTextColor(new Color(0xffffb90f));
                flag_layout = true;
            } else {
                hagstrom_checkbox.setImageAndDecodeBounds(ResourceTable.Media_checkbox_false);
                hagstrom_text.setTextColor(new Color(0x99999999));
                flag_layout = false;
            }
        });
        lbanez_lin.setClickedListener(component -> {
            if (lbanez_checkbox != null && flag_layout == false) {
                lbanez_checkbox.setImageAndDecodeBounds(ResourceTable.Media_checkbox_true);
                flag_layout = true;
            } else {
                lbanez_checkbox.setImageAndDecodeBounds(ResourceTable.Media_checkbox_false);
                flag_layout = false;
            }
        });
    }

    private void initRadiobutton() {
        radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_radiocontainer);
        radioButton_one = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_one);
        radioButton_one.setChecked(false);
        radioButton_three = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_three);
        radioButton_two = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_two);
        Element button_element = null;
        radioButton_one.setButtonElement(button_element);
        radioButton_three.setButtonElement(button_element);
        VectorElement radiobutton_element = new VectorElement(getContext(), ResourceTable.Graphic_ic_new_releases_black_24dp);
        VectorElement radiobutton_element_email = new VectorElement(getContext(), ResourceTable.Graphic_ic_email_black_24dp);
        VectorElement radiobutton_element_person = new VectorElement(getContext(), ResourceTable.Graphic_ic_person_black_24dp);
        radioButton_one.setBackground(radiobutton_element);
        radioButton_two.setBackground(radiobutton_element_email);
        radioButton_three.setBackground(radiobutton_element_person);
        radioButton_two.setButtonElement(button_element);

        radioContainer.setMarkChangedListener((radioContainer, i) -> {
            if (radioButton_one.isChecked()) {
                radioButton_one.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_click_menu_one));
            } else {
                radioButton_one.setBackground(radiobutton_element);
            }
            if (radioButton_three.isChecked()) {
                radioButton_three.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_click_menu_three));
            } else {
                radioButton_three.setBackground(radiobutton_element_person);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
